﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core
{
    public class MapPhantom
    {

        /// <summary>
        /// Initializes structures to keep mapping between phantom and real Ids
        /// and lists of implicitly updated and removed records dictionaries.
        /// </summary>
        public void InitRowsHolders()
        {
            AddedIds = new Dictionary<String, IDictionary<string, int>>();

            AddedIds.Add("tasks", new Dictionary<String, int>());
            AddedIds.Add("calendars", new Dictionary<String, int>());
            AddedIds.Add("resources", new Dictionary<String, int>());

            UpdatedRows = new Dictionary<String, IDictionary<int, IDictionary<string, object>>>();

            UpdatedRows.Add("tasks", new Dictionary<int, IDictionary<string, object>>());
            UpdatedRows.Add("calendars", new Dictionary<int, IDictionary<string, object>>());
            UpdatedRows.Add("resources", new Dictionary<int, IDictionary<string, object>>());

            RemovedRows = new Dictionary<String, IDictionary<int, IDictionary<string, object>>>();

            RemovedRows.Add("tasks", new Dictionary<int, IDictionary<string, object>>());
            RemovedRows.Add("calendars", new Dictionary<int, IDictionary<string, object>>());
            RemovedRows.Add("calendardays", new Dictionary<int, IDictionary<string, object>>());
            RemovedRows.Add("resources", new Dictionary<int, IDictionary<string, object>>());
            RemovedRows.Add("assignments", new Dictionary<int, IDictionary<string, object>>());
            RemovedRows.Add("dependencies", new Dictionary<int, IDictionary<string, object>>());
        }


        /// <summary>
        /// A dictionary keeping phantom to real Id references for all tables.
        /// </summary>
        public IDictionary<String, IDictionary<String, int>> AddedIds { get; set; }

        /// <summary>
        ///                                    被删除的Id，实际无用，但下面的dict，key总是为Id,所以这里要做个拆分。。。为了json可以生成【"Id":1,"Id":2】
        ///              task|dependencies     taskIdVal           Id    taskIdVal
        /// </summary>
        public IDictionary<String, IDictionary<int, IDictionary<string, object>>> RemovedRows { get; set; }

        public IDictionary<String, IDictionary<int, IDictionary<string, object>>> UpdatedRows { get; set; }







        public bool HasUpdatedRows(String table)
        {
            return UpdatedRows.ContainsKey(table) && UpdatedRows[table].Count > 0;
        }

        public IList<IDictionary<string, object>> GetUpdatedRows(String table)
        {
            if (!HasUpdatedRows(table)) return null;

            return UpdatedRows[table].Values.ToList();
        }

        public bool HasRemovedRows(String table)
        {
            return RemovedRows.ContainsKey(table) && RemovedRows[table].Count > 0;
        }

        public IList<IDictionary<string, object>> GetRemovedRows(String table)
        {
            if (!HasRemovedRows(table)) return null;

            return RemovedRows[table].Values.ToList();
        }

        /// <summary>
        /// Gets real record identifier matching specified phantom one.
        /// </summary>
        /// <param name="table">Table name.</param>
        /// <param name="phantomId">Phantom identifier.</param>
        /// <returns>Real record identifier.</returns>
        public int? getIdByPhantom(String table, String phantomId)
        {
            if (!AddedIds.ContainsKey(table)) return null;
            IDictionary<String, int> map = AddedIds[table];
            if (map == null) return null;

            // get real task Id
            if (phantomId != null && map.ContainsKey(phantomId))
            {
                return map[phantomId];
            }

            return null;
        }

        /// <summary>
        /// Gets real task identifier by specified phantom one.
        /// </summary>
        /// <param name="phantomId">Task phantom identifier.</param>
        /// <returns>Task real identifier.</returns>
        public int? getTaskIdByPhantom(String phantomId)
        {
            return getIdByPhantom("tasks", phantomId);
        }

        /// <summary>
        /// Gets real resource identifier by specified phantom one.
        /// </summary>
        /// <param name="phantomId">Resource phantom identifier.</param>
        /// <returns>Resource real identifier.</returns>
        public int? getResourceIdByPhantom(String phantomId)
        {
            return getIdByPhantom("resources", phantomId);
        }

        /// <summary>
        /// Gets real calendar identifier by specified phantom one.
        /// </summary>
        /// <param name="phantomId">Calendar phantom identifier.</param>
        /// <returns>Calendar real identifier.</returns>
        public int? getCalendarIdByPhantom(String phantomId)
        {
            return getIdByPhantom("calendars", phantomId);
        }





    }
}
